package com.chinapost.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.chinapost.service.DeptServiceI;
import com.chinapost.service.FczyServiceI;
import com.chinapost.vo.Json;
import com.chinapost.vo.SessionInfo;
import com.chinapost.vo.Fczy;
import com.opensymphony.xwork2.ModelDriven;

@Action(value = "fczyAction", results = { 
		@Result(name = "fczyManager", location = "/jsp/fczy/fczyManager.jsp"),
		@Result(name = "fczyAdd", location = "/jsp/fczy/fczyAdd.jsp"), 
		@Result(name = "fczyImport", location = "/jsp/fczy/fczyImport.jsp"),
		@Result(name = "fczyEdit", location = "/jsp/fczy/fczyEdit.jsp") 
		})
public class FczyAction extends BaseAction implements ModelDriven<Fczy>, ServletRequestAware {

	private Fczy fczy = new Fczy();
	private FczyServiceI fczyService;
	private DeptServiceI deptService;
	private HttpServletRequest request;

	public DeptServiceI getDeptService() {
		return deptService;
	}

	@Autowired
	public void setDeptService(DeptServiceI deptService) {
		this.deptService = deptService;
	}

	public FczyServiceI getFczyService() {
		return fczyService;
	}

	@Autowired
	public void setFczyService(FczyServiceI fczyService) {
		this.fczyService = fczyService;
	}

	@Override
	public Fczy getModel() {
		return fczy;
	}

	public String fczyManager() {
		return "fczyManager";
	}

	public String fczyEdit() {
		return "fczyEdit";
	}

	public String fczyAdd() {
		return "fczyAdd";
	}

	public String fczyImport() {
		return "fczyImport";
	}


	public void add() {
		Json j = new Json();
		try {
			fczyService.add(fczy);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}

	public void addFczys() {
		Json j = new Json();
		int count = 0;
		try {
			count = fczyService.addFczys(fczy);
			if (count > 0) {
				j.setSuccess(true);
				j.setMsg("上传成功，新增" + count + "条记录！");
			} else {
				j.setMsg("上传失败,新增0条记录！");
			}
		} catch (Exception e) {
			j.setMsg("上传失败,新增0条记录！");
		}
		super.writeJson(j);
	}

	public void edit() {
		Json j = new Json();
		try {
			fczyService.edit(fczy);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			j.setMsg("编辑失败！");
		}
		writeJson(j);
	}

	public void delete() {
		Json j = new Json();
		try {
			fczyService.delete(fczy);
			j.setSuccess(true);
			j.setMsg("删除成功！");
			j.setObj(fczy.getCid());
		} catch (Exception e) {
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}

	public void datagrid() {
		if (!StringUtils.isEmpty(fczy.getDeptId())) {
			fczy.setDeptIds(deptService.getChildIds(fczy.getDeptId()));
		} else {
			SessionInfo s = (SessionInfo)request.getSession().getAttribute("sessionInfo");
			fczy.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}

		super.writeJsonDate(fczyService.datagrid(fczy));
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}
}
